package com.ship.service;

import java.util.List;

import com.ship.control.search.ConditionPart;
import com.ship.control.search.SearchCond;
import com.ship.entity.Dept;
import com.ship.entity.User;

/**
 * 人员服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
public class UserService extends BaseService{

	/**
	 * 编辑用户信息 (for dwr)
	 * @param user
	 */
	public void editUser(User user){
		if(null==user.getId()){
			user.setPass("111111");
			this.addUser(user);
		}else{
			User oldPeople=this.dao.loadObject(User.class, user.getId());
			user.setPass(oldPeople.getPass());
			this.updateUser(user);
		}
	}
	
	
	public void addUser(User people){
		this.dao.saveObject(people);
	}
	
	
	public void updateUser(User people){
		this.dao.updateObject(people);
	}
	
	
	public void resetPass(Long id){
		User people=this.dao.loadObject(User.class, id);
		people.setPass("111111");
		this.dao.updateObject(people);
	}
	
	
	/**
	 * 删除用户 （for dwr）
	 * @param id
	 */
	public void delUserById(Long id){
		User people=this.dao.loadObject(User.class, id);
		if(people!=null){
			this.dao.deleteObject(people);
		}
	}
	
	
	/**
	 * 获取部门列表
	 * @param searchCond
	 * @return
	 */
	public List<User> getUserList(SearchCond searchCond){
		ConditionPart condPart = searchCond.getCondition();
		Object params[]=condPart.getParams().toArray(new Object[0]);
		List<User> peopleList=this.dao.loadObjects(User.class,condPart.getExpression(), "id desc", searchCond.getPageSize(), searchCond.getPageIndex(), params);
		return peopleList;
	}
	
	/**
	 * 获取用户 （for dwr）
	 * @param id
	 * @return
	 */
	public User getUserById(Long id){
		User user= this.dao.loadObject(User.class, id);
		return user;
	}
	
	
	public boolean hasSign(String sign){
		User user=this.dao.loadObject(User.class, "sign=?",sign);
		if(null!=user){
			return true;
		}else{
			return false;
		}
		
	}
	
	
	public User getUserBySign(String sign){
		User user=this.dao.loadObject(User.class, "sign=?", sign);
		return user;
	}
	
	
	//============================================================
	/**
	 * 编辑部门 （for dwr）
	 * @param dept
	 */
	public void editDept(Dept dept){
		if(null==dept.getId()){
			this.addDept(dept);
		}else{
			this.updateDept(dept);
		}
	}
	
	/**
	 * 添加部门
	 * @param dept
	 */
	public void addDept(Dept dept){
		this.dao.saveObject(dept);
	}
	
	/**
	 * 修改部门
	 * @param dept
	 */
	public void updateDept(Dept dept){
		this.dao.updateObject(dept);
	}
	
	/**
	 * 删除部门
	 * @param id
	 */
	public void delDept(Long id){
		Dept dept=this.dao.loadObject(Dept.class, id);
		this.dao.deleteObject(dept);
	}
	
	/**
	 * 获取部门列表
	 * @param searchCond
	 * @return
	 */
	public List<Dept> getDeptList(SearchCond searchCond){
		ConditionPart condPart = searchCond.getCondition();
		Object params[]=condPart.getParams().toArray(new Object[0]);
		List<Dept> departmentList=this.dao.loadObjects(Dept.class,condPart.getExpression(), "id", searchCond.getPageSize(), searchCond.getPageIndex(), params);
		return departmentList;
	}
	
	/**
	 * 获取所有的部门
	 * @return
	 */
	public List<Dept> getAllDept(){
		List<Dept> deptList=this.dao.loadObjects(Dept.class, null, "id asc", null, null);
		return deptList;
	}
	
	/**
	 * 根据ID查部门 （for dwr）
	 * @param id
	 * @return
	 */
	public Dept getDeptById(Long id) {
		return this.dao.loadObject(Dept.class, id);
	}
	
	
	
}
